package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type OperateMonitorTaskEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	TemplateName string // 模板名称
	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名

	TemplateParam []map[string]interface{} // 模板参数
	DeviceIds     []int64                  // 设备ID
	TaskParam     map[string]interface{}   // 任务参数
}

type operateMonitorTaskPo struct {
	Id            int64                    `json:"id" db:"id"`                        // 对象id
	CreateTime    int64                    `json:"createTime" db:"create_time"`       // 创建时间
	UpdateTime    int64                    `json:"updateTime" db:"update_time"`       // 更新时间
	Manufacturer  string                   `json:"manufacturer" db:"manufacturer"`    // 设备厂商名称
	DeviceType    string                   `json:"deviceType" db:"device_type"`       // 设备类型名
	TemplateName  string                   `json:"templateName" db:"template_name"`   // 设备名称
	TemplateParam []map[string]interface{} `json:"templateParam" db:"template_param"` // 配置参数
	DeviceIds     []int64                  `json:"deviceIds" db:"device_ids"`         // 扩展参数
	TaskParam     map[string]interface{}   `json:"taskParam" db:"task_param"`         // 通道名称
}

func (e *OperateMonitorTaskEntity) Type() string {
	return "OperateMonitorTaskEntity"
}

func (e *OperateMonitorTaskEntity) TableName() string {
	return "tb_operate_monitor_task"
}

func (e *OperateMonitorTaskEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *OperateMonitorTaskEntity) Instance() *OperateMonitorTaskEntity {
	return &OperateMonitorTaskEntity{
		BaseEntity:    new(commEntity.BaseEntity),
		TemplateParam: make([]map[string]interface{}, 0),
		DeviceIds:     make([]int64, 0),
		TaskParam:     make(map[string]interface{}),
	}
}

func (e *OperateMonitorTaskEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *OperateMonitorTaskEntity) NewPo() interface{} {
	return &operateMonitorTaskPo{
		TemplateParam: make([]map[string]interface{}, 0),
		DeviceIds:     make([]int64, 0),
	}
}

func (e *OperateMonitorTaskEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *OperateMonitorTaskEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.TemplateName)
	return p
}

func (e *OperateMonitorTaskEntity) BuildByJson(jsn *string) error {
	var po operateMonitorTaskPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *OperateMonitorTaskEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *OperateMonitorTaskEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*operateMonitorTaskPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.TemplateParam = po.TemplateParam
	ptr.DeviceIds = po.DeviceIds
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.TemplateName = po.TemplateName
	ptr.TaskParam = po.TaskParam
}

func (e *OperateMonitorTaskEntity) BuildToPo() interface{} {
	ptr := e
	po := operateMonitorTaskPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.TemplateParam = ptr.TemplateParam
	po.DeviceIds = ptr.DeviceIds
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.TemplateName = ptr.TemplateName
	po.TaskParam = ptr.TaskParam

	return &po
}
